import React, {Component} from "react";
import PubSub from 'pubsub-js'
import {nanoid} from "nanoid";

export default class Header extends Component{
    //触发回车事件
    handleEnter=(e)=>{
        const {target, keyCode}=e
        //回车或者输入不等于空才触发
        if(keyCode!==13||target.value.trim()===''){
            return
        }
        let mainObj={id:nanoid(),value:target.value,done:false}
        PubSub.publish('addMainItem',mainObj)
        //恢复默认值
        e.target.value=''
    }
    render(){
        return (
            <div className="todo-header">
                <input type="text" placeholder="请输入你的任务名称，按回车键确认" onKeyUp={this.handleEnter}/>
            </div>
        )
    }
}
